home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / Std I⁄O for C / sf_stdio.c < prev    next >
Text File  |  1988-03-02  |  3KB  |  129 lines

  1. /* sf_stdio.c 
  2.  * 
  3.  * Chris Borton, 27 February 1988
  4.  * some functions taken/adapted from BuggyEdit.c demo with LS C 
  5.  * Provides a SFFile interface to reset stdin/stdout streams
  6.  */
  7.  
  8. #include <MacTypes.h>
  9. #include <FileMgr.h>
  10. #include <StdFilePkg.h>
  11. #include <pascal.h>
  12. #include <proto.h>
  13.  
  14. #include <stdio.h>
  15. #include "sf_stdio.h"
  16.  
  17. /* globals used in this file */
  18. static Point SFGwhere = { 90, 82 };            /* these shouldn't be hard-coded… */
  19. static Point SFPwhere = { 106, 104 };
  20. static SFReply reply;
  21.  
  22. /* copies a pascal string from p1 to p2, like strcpy() */
  23. void pStrCpy(char *p2, char *p1 );
  24. void pStrCpy( p2, p1 )
  25. register char *p2, *p1;
  26. {
  27.     register int len;
  28.     
  29.     len = *p2++ = *p1++;
  30.     while (--len >= 0) 
  31.         *p2++=*p1++;
  32. }
  33.  
  34. /* use StdGetFile to choose text file and return with vRef & file name */
  35. Boolean ChooseFile( Str255 *fn, int *vRef, Str255 msg);
  36. static Boolean ChooseFile( fn, vRef, msg)
  37. Str255    *fn;
  38. int        *vRef;
  39. Str255    msg;
  40. {
  41.     SFTypeList    myTypes;
  42.     
  43.     myTypes[0]='TEXT';
  44.     SFGetFile( SFGwhere, msg, 0L, 1, myTypes, 0L, &reply );
  45.     if (reply.good) {
  46.         pStrCpy( (char *)fn , (char *)reply.fName);
  47.         *vRef = reply.vRefNum;
  48.         return(TRUE);
  49.     }
  50.     else return(FALSE);
  51. }
  52.  
  53. /* get name and directory to save a new file in */
  54. Boolean PutFile( Str255 *fn, int *vRef, Str255 msg);
  55. static Boolean PutFile( fn, vRef, msg)
  56. Str255    *fn;
  57. int        *vRef;
  58. Str255    msg;
  59. {
  60.     SFPutFile(SFPwhere, msg, fn, 0L, &reply);
  61.     if (reply.good) {
  62.         pStrCpy( (char *) fn, (char *) reply.fName);
  63.         *vRef = reply.vRefNum;
  64.         return(TRUE);
  65.     }
  66.     else return(FALSE);
  67. }
  68.  
  69.  
  70. /* sf_stdin()
  71.  *
  72.  * reset stdin stream to read from a text file, chosen with SFGetFile
  73.  */
  74. Boolean sf_stdin()
  75. {
  76.     Str255         message, sf_stdin_name;
  77.     int            vRef;                    /* just a dummy arg here */
  78.  
  79.     pStrCpy((char *)&message, "\pInput file for stdin:");    /* this doesn't get shown */
  80.     if (ChooseFile(&sf_stdin_name, &vRef, message)) {
  81.         PtoCstr((char *)(&sf_stdin_name));
  82.         
  83.         if (freopen ((char *)&sf_stdin_name, "r", stdin) == NULL) {
  84.             SysBeep(10);                    /* error somewhere! */
  85.             fprintf (stderr, "can't open %s\n", sf_stdin_name);
  86.             return FALSE;
  87.         }
  88.         else
  89.             return TRUE;
  90.  
  91.     }
  92.     else {
  93.         PtoCstr((char *)(&sf_stdin_name));
  94.         fprintf(stderr,"Cancel button hit, filename = \"%s\"\n",sf_stdin_name);
  95.         return FALSE;
  96.     }
  97. }
  98.  
  99. /* sf_stdout()
  100.  *
  101.  * reset stdout stream to read from a text file, chosen with SFPutFile
  102.  */
  103. Boolean sf_stdout()
  104. {
  105.     Str255         message, sf_stdout_name;
  106.     int            vRef;                    /* just a dummy arg here */
  107.  
  108.     pStrCpy((char *)(&message), "\pOutput file for stdout:");    
  109.     pStrCpy((char *)(&sf_stdout_name), "\psf_stdout_file");    
  110.     
  111.     if (PutFile(&sf_stdout_name, &vRef, message)) {
  112.         PtoCstr((char *)(&sf_stdout_name));
  113.         
  114.         if (freopen ((char *)&sf_stdout_name, "w", stdout) == NULL) {
  115.             SysBeep(10);                    /* error somewhere! */
  116.             fprintf (stderr, "Can't open file \"%s\", errno = %d\n", sf_stdout_name,errno);
  117.             return FALSE;
  118.         }
  119.         else
  120.             return TRUE;
  121.  
  122.     }
  123.     else {
  124.         PtoCstr((char *)(&sf_stdout_name));
  125.         fprintf(stderr,"Cancel button hit, filename = \"%s\"\n",sf_stdout_name);
  126.         return FALSE;
  127.     }
  128. }
  129.